package com.solver.demo.rscoket.v2.controller;

import com.solver.demo.rscoket.v2.api.ChannelMessage;
import com.solver.demo.rscoket.v2.server.MessageProcessor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.stereotype.Controller;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Controller
@Slf4j
@RequiredArgsConstructor
public class V2RSocketController {

    @MessageMapping("request-responseV2")
    public Mono<String> requestResponse(String request) {
        return Mono.just("Response to " + request);
    }

    @MessageMapping("request-streamV2")
    public Flux<String> requestStream(String request) {
        return Flux.just("Stream response 1", "Stream response 2", "Stream response 3");
    }

    @MessageMapping("fire-and-forgetV2")
    public Mono<Void> fireAndForget(String request) {
        System.out.println("Received: " + request);
        return Mono.empty();
    }

    // 使用正确的依赖类型
    private final MessageProcessor messageProcessor;

    // 其他方法保持不变
    @MessageMapping("channelV2")
    public Flux<String> handleChannel(Flux<ChannelMessage> messages) {
        return messages
                .doOnNext(msg -> log.debug("Received group {} message: {}", msg.getGroupId(), msg.getContent()))
                .flatMap(messageProcessor::process)
                .onErrorResume(e -> {
                    log.error("Channel processing failed: {}", e.getMessage());
                    return Flux.just("Error: " + e.getMessage());
                });
    }
}
